﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChinaDynastyTours.Controls;

namespace ChinaDynastyTours.Views
{
    public partial class SalePriceForm : Form
    {
        //个人对外报价百分比
        decimal _iProfitRate;
        //存储各个数据列计算的和值，第一个int是列号，第二个int为列和
        Dictionary<int, decimal?> numberRectanger = new Dictionary<int, decimal?>();
        private string _TourgroupID;

        public SalePriceForm(string tourgroupid)
        {
            InitializeComponent();

            _TourgroupID = tourgroupid;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //this.gv_PriceList.Columns["ComplexCost"].Visual=false;
            _iProfitRate = Convert.ToDecimal(pceProfitRate.EditValue);
            this.gv_PriceList.Columns["ComplexCost"].ColumnEdit.EditValueChanged += new EventHandler(ComplexCost_EditValueChanged);
            this.gv_PriceList.Columns["MealCost"].ColumnEdit.EditValueChanged += new EventHandler(ComplexCost_EditValueChanged);
            this.gv_PriceList.Columns["OverKMCost"].ColumnEdit.EditValueChanged += new EventHandler(ComplexCost_EditValueChanged);
            this.gv_PriceList.Columns["RoomCost"].ColumnEdit.EditValueChanged += new EventHandler(ComplexCost_EditValueChanged);
            this.gv_PriceList.Columns["TicketsCost"].ColumnEdit.EditValueChanged += new EventHandler(ComplexCost_EditValueChanged);
            this.gv_PriceList.Columns["CitiesTrafficCost"].ColumnEdit.EditValueChanged += new EventHandler(ComplexCost_EditValueChanged);


            this.gc_PricList.DataSource = SalePriceCtrl.GetAllData();
            this.gc_PricList.DataMember = "teamPriceDt";

            LoadData();
        }


        void ComplexCost_EditValueChanged(object sender, EventArgs e)
        {
            //获取数据源
            DataView ds = gv_PriceList.DataSource as DataView;

            //循环数据源数据加和，焦点行特殊，需获取当前编辑值
            if (ds.Table.Rows.Count > 0)
            {
                decimal iTotalCost = 0;
                int iFocuseColumn = gv_PriceList.FocusedColumn.ColumnHandle;
                for (int i = 0; i < ds.Table.Rows.Count; i++)
                {
                    if (gv_PriceList.FocusedRowHandle == i && gv_PriceList.FocusedRowModified)
                        iTotalCost += Convert.ToDecimal((sender as CalcEdit).Value.ToString());
                    else
                        iTotalCost += Convert.ToDecimal(ds.Table.Rows[i][iFocuseColumn].ToString());
                }
                //存储对应列的数据和
                if (numberRectanger.ContainsKey(iFocuseColumn))
                    numberRectanger[iFocuseColumn] = iTotalCost;
                else
                    numberRectanger.Add(iFocuseColumn, iTotalCost);
                
                //显示各列和
                switch (iFocuseColumn)
                {
                    case 2:
                        lbComplexCost.Text = iTotalCost.ToString();
                        break;
                    case 3:
                        lbMealCost.Text = iTotalCost.ToString();
                        break;
                    case 4:
                        lbOverKMCost.Text = iTotalCost.ToString();
                        break;
                    case 5:
                        lbRoomCost.Text = iTotalCost.ToString();
                        break;
                    case 6:
                        lbTicketsCost.Text = iTotalCost.ToString();
                        break;
                    case 7:
                        lbCitiesTrafficCost.Text = iTotalCost.ToString();
                        break;
                    default:
                        break;
                }
            }
            //计算个人底价和个人对外报价
            decimal? iAverageValue = 0;
            foreach (var item in numberRectanger)
            {
                if (item.Value != null)
                    iAverageValue += item.Value;
            }
            lbAverageCost.Text = string.Format("{0}", iAverageValue);
            //_iProfitRate = Convert.ToDecimal(pceProfitRate.EditValue);
            //calcAveragePrice.Text = string.Format("{0}", iAverageValue * _iProfitRate + iAverageValue);
            
        }

        private void LoadData()
        {
            SalePriceCtrl.ClearAllData();
            numberRectanger.Clear();

            bool bValue = SalePriceCtrl.LoadAllData(_TourgroupID);
            if (!bValue)
                MessageBox.Show("打开的文件数据非法，请找到正确的数据文件重新加载", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                //数据加载成功后，刷新各统计结果
                DataView ds = gv_PriceList.DataSource as DataView;

                //初始化巨型统计数组
                foreach (DataRow row in ds.Table.Rows)
                {
                    for (int i = 2; i <=7; i++)
		            {
                        if (numberRectanger.ContainsKey(i))
                            numberRectanger[i] += Convert.ToDecimal(row[i].ToString());
                        else
                            numberRectanger.Add(i, Convert.ToDecimal(row[i].ToString()));
		            }
                }

                //计算个人底价和个人对外报价
                decimal? iAverageValue = 0;
                foreach (var item in numberRectanger)
                {
                    if (item.Value != null)
                    {
                        if (item.Key == 2) lbComplexCost.Text=item.Value.ToString();
                        if (item.Key == 3) lbMealCost.Text = item.Value.ToString();
                        if (item.Key == 4) lbOverKMCost.Text = item.Value.ToString(); 
                        if (item.Key == 5) lbRoomCost.Text = item.Value.ToString();
                        if (item.Key == 6) lbTicketsCost.Text = item.Value.ToString();
                        if (item.Key == 7) lbCitiesTrafficCost.Text = item.Value.ToString();

                        iAverageValue += item.Value;
                    }
                }
                lbAverageCost.Text = string.Format("{0}", iAverageValue);
                //_iProfitRate = Convert.ToDecimal(pceProfitRate.EditValue);
                //calcAveragePrice.Text = string.Format("{0}", iAverageValue * _iProfitRate + iAverageValue);
                DataSet dataS = gc_PricList.DataSource as DataSet;
                if (dataS.Tables["teamAverageDt"].Rows.Count > 0)
                {
                    tbCurrency.Text = dataS.Tables["teamAverageDt"].Rows[0]["Currency"].ToString();
                    tbCurrencyAmount.EditValue = dataS.Tables["teamAverageDt"].Rows[0]["CurrencyAmount"];
                    tbExchangeRate.EditValue = dataS.Tables["teamAverageDt"].Rows[0]["ExchangeRate"];
                }

            }
        }


        #region 个人对外报价数额或比率

        private void tbExchangeRate_EditValueChanged(object sender, EventArgs e)
        {
            decimal exchangeRate = tbExchangeRate.EditValue == null ? 1 : decimal.Parse(tbExchangeRate.EditValue.ToString());
            decimal paymentAmount = tbCurrencyAmount.EditValue == null ? 0 : decimal.Parse(tbCurrencyAmount.EditValue.ToString());

            decimal cost = Convert.ToDecimal(lbAverageCost.Text);// 每人底价合计
            decimal price = exchangeRate * paymentAmount;//每人对外报价合计

            calcAveragePrice.EditValue = price;
            if (cost != 0)
            {
                _iProfitRate = (price - cost) / cost;
                this.pceProfitRate.EditValue = (price - cost) / cost;
            }
        }
        #endregion
 
    }
}
